﻿var CommonFunction = {
    submitLogin: function () {
        window.frmLogin.getForm().submit({
            waitMsg: 'Đang kiểm tra...',
            waitTitle: 'Xác thực quyền đăng nhập',
            clientValidation: true,
            success: CommonFunction.refreshFrame,
            failure: CommonMessenger.failureHandler
        });
    },
    loadPage: function (controller, action, params) {
        var pnl = window.parent.mainPanel;
        pnl.load({
            url: '/' + controller + '/' + action,
            params: params
        });
        //        var mnpnl = window.parent.menuPanel;
        //        mnpnl.load({
        //            url: '/Menu/' + menuAction,
        //            params: params
        //        });
    },
    submit: function (params) {
        var frm = window.frmMain;
        frm.form.submit({
            waitMsg: 'Đang lưu dữ liệu...',
            waitTitle: 'Thông báo',
            timeout: 60000,
            success: function () { CommonMessenger.successNotice('Thông báo', 'Lưu dữ liệu thành công'); },
            failure: CommonMessenger.failureHandler,
            params: params
        });
    },
    submitnreview: function (params, controller, action, p) {
        var frm = window.frmMain;
        frm.form.submit({
            waitMsg: 'Đang lưu dữ liệu...',
            waitTitle: 'Thông báo',
            timeout: 60000,
            success: function () { CommonFunction.loadPage(controller, action, p); },
            failure: CommonMessenger.failureHandler,
            params: params
        });
    },
    addTab: function (tabPanel, id, url, title) {
        var tp = tabPanel;
        var tab = tp.getComponent(id);
        title = (title.length > 30) ? title.substring(0, 29) + '..' : title;
        if (!tab) {
            tab = tp.add({
                id: id,
                title: title,
                closable: true,
                autoLoad: {
                    showMask: true,
                    url: url,
                    mode: "iframe",
                    maskMsg: "Loading " + title + "..."
                }
            });
        }
        tp.setActiveTab(tab);
    },

    addTab1: function (tabPanel, id, url, title, maloai) {
        var tp = tabPanel;
        var tab = tp.getComponent(id);
        title = (title.length > 30) ? title.substring(0, 29) + '..' : title;
        if (!tab) {
            tab = tp.add({
                id: id,
                title: title,
                closable: true,
                autoLoad: {
                    showMask: true,
                    url: url,
                    params: {
                        MaLoai: maloai
                    },
                    mode: "iframe",
                    maskMsg: "Loading " + title + "..."
                }
            });
        }
        tp.setActiveTab(tab);
    },

    addTab2: function (id, url, title, isAdd, maloai, grd) {
        var tp = window.parent.parent.tabPanelMain;
        var tab = tp.getComponent(id);
        title = (title.length > 30) ? title.substring(0, 29) + '..' : title;
        if (!tab) {
            tab = tp.add({
                id: id,
                title: title,
                closable: true,
                autoLoad: {
                    showMask: true,
                    url: url,
                    params: {
                        MaLoai: maloai == '' ? '' : maloai.getValue(),
                        ID: isAdd ? '' : grd.selModel.getSelected().data.ID
                    },
                    mode: "iframe",
                    maskMsg: "Loading " + title + "..."
                }
            });
        }
        tp.setActiveTab(tab);
    },

    XemBaoCao: function (id, url, title, params) {
        var tp = window.parent.parent.tabPanelMain;
        var tab = tp.getComponent(id);
        title = (title.length > 30) ? title.substring(0, 29) + '..' : title;
        if (!tab) {
            tab = tp.add({
                id: id,
                title: title,
                closable: true,
                autoLoad: {
                    showMask: true,
                    url: url,
                    params: params,
                    mode: "iframe",
                    maskMsg: "Loading " + title + "..."
                }
            });
        }
        tp.setActiveTab(tab);
    },

    popup: function (title, url, isAdd, grd) {
        window.windowChild.setTitle(title);
        window.windowChild.load({
            url: url,
            params: {
                ID: isAdd ? '' : grd.selModel.getSelected().data.ID
            }
        });
        window.windowChild.show();
    },

    refreshFrame: function () {
        location.reload();
    },

    CloseWindow: function () {
        window.parent.windowChild.hide();
    },

    closeTab: function () {
        var tp = window.parent.parent.tabPanelMain;
        tp.activeTab.destroy();
    },

    setColumnHidden: function (grid, columnID, hidden) {
        //hidden is boolean type
        var index = grid.colModel.getIndexById(columnID);
        grid.colModel.setHidden(index, hidden);
    },

    setColumnHeader: function (grid, columnID, header) {
        //header is string type
        var index = grid.colModel.getIndexById(columnID);
        grid.colModel.setColumnHeader(index, header);
    },

    renderDate: function (value) {
        return value == '' ? '' : eval(value.replace(/\/Date\((.*?)\)\//gi, "new Date($1)")).format('d/m/Y');
    },

    renderGioitinh: function (value) {
        if (value == "NAM") return "Nam";
        else if (value == "NU") return "Nữ";
    },

    renderMoney: function (value) {
        if (value == null) return '';
        var minus = '';
        value = value.toString().replace('.', ',');
        if (value.substring(0, 1) == '-') {
            value = value.substring(1);
            minus = '-';
        };
        var iIndex = value.indexOf(',');
        var sf = (iIndex == -1 ? '' : value.substring(iIndex));
        value = (iIndex == -1 ? value : value.substring(0, iIndex));
        var money = '';
        for (var i = 0; i < value.length; i++) {
            if (i > 0 && (value.length - i) % 3 == 0) money += '.';
            money += value.charAt(i);
        }
        if (money.length == 0) money = '0';
        return "<div style='text-align: right;'>" + minus + money + sf + "</div>";
    },

    viewMoney: function (value) {
        if (value != null && value.toString().length > 0) {
            value = value + '';
            var money = '';
            for (var i = 0; i < value.length; i++) {
                if (i > 0 && (value.length - i) % 3 == 0)
                    money += '.';
                money += value.charAt(i);
            }
        } else {
            money = ' ';
        }
        return money;
    }

};

var CommonMessenger = {
    successNotice: function (title, msg) {
        window.Ext.net.Notification.show({
            //iconCls: 'icon-information',
            //icon: windown.Ext.Net.Icon.Information,
            icon: window.Ext.Msg.INFO,
            hideDelay: 3000,
            autoHide: true,
            closeVisible: true,
            html: msg,
            title: title,
            alignToCfg: {
                offset: [0, 20],
                position: 'tr-tr'
            },
            showFx: {
                args: ['t', {}],
                fxName: 'slideIn'
            },
            hideFx: {
                args: ['t', {}],
                fxName: 'ghost'
            }
        });
    },
    successHandler: function (form, action) {
        if (action.result && action.result.msg) {
        }
        CommonFunction.CloseWindow();
        //successNotice('Thông báo', 'Lưu dữ liệu thành công');
    },

    failureHandler: function (form, action) {
        var msg = '';

        if (action.failureType == 'client' || (action.result && action.result.errors && action.result.errors.length > 0)) {
            msg = "Không nhập đủ thông tin";
        } else if (action.result && action.result.msg) {
            msg = action.result.msg;
        } else if (action.response) {
            msg = action.response.responseText;
        }
        CommonMessenger.showErrorMessage(msg);
    },

    showInfoMessage: function (msg) {
        window.Ext.Msg.show({
            title: Message.Info,
            msg: msg,
            buttons: window.Ext.Msg.OK,
            icon: window.Ext.Msg.INFO
        });
    },

    showErrorMessage: function (msg) {
        window.Ext.Msg.show({
            title: Message.Error,
            msg: msg,
            buttons: window.Ext.Msg.OK,
            icon: window.Ext.Msg.ERROR
        });
    },

    showWarningMessage: function (msg) {
        window.Ext.Msg.show({
            title: Message.Warning,
            msg: msg,
            buttons: window.Ext.Msg.OK,
            icon: window.Ext.Msg.WARNING
        });
    }
};